package team.gs.model;

import java.io.IOException;
import java.util.Vector;

import org.andengine.entity.text.Text;

import android.content.res.AssetManager;

/**
 * 
 * @author JVM
 */
public class Method {
	public static int title_height = 25;
	public static int lineSpacing = 15;
	public static int rightAlign = 5;
	public static int leftAlign = 5;

	public static int topAlign = 5;

	public static int bottomAlign = 5;

	public static Vector getDataLines(int canvas, AssetManager asset,
			String path, Text txtline) {
		try {
			readFile rf = new readFile();
			return Method.formatLines(
					rf.split(rf.readUnicodeFile(asset, path), "\n"), canvas,
					txtline);
		} catch (IOException e) {
			System.out.println("method : 2");
			return null;
		}
	}

	public static Vector getlineBox(int canvas, String str, Text txtline) {
		readFile rf = new readFile();
		return Method.formatLines(rf.split(str, "\n"), canvas, txtline);
	}

	// dinh dang lai cac dong quá màn hình
	public static Vector formatLines(String[] lines, int maxWidth, Text txtline) {
		Vector v = new Vector();
		// int vungve = maxWidth - anchor.leftAlign - anchor.rightAlign - 10;
		int vungve = maxWidth + 20;
		int width;
		String[] temp;
		String s;
		for (int i = 0; i < lines.length; i++) {
			// lay do dai cua dong can kiem tra
			txtline.setText(lines[i]);
			width = (int) txtline.getWidth();
			if (width <= vungve) {
				v.addElement(lines[i]);
			} else {
				// cat dong thanh mảng các từ
				temp = readFile.split(lines[i], " ");
				s = "";
				for (int j = 0; j < temp.length; j++) {
					if (getWidth(s + temp[j] + " ", txtline) < vungve) {
						s += temp[j] + " ";
					} else {
						// cat chuoi them vao
						v.addElement(s);
						s = temp[j] + " ";
					}
					// chuoi cuoi cung cho vao
					if (j == temp.length - 1) {
						v.addElement(s);
					}
				}
			}
		}
		return v;
	}

	public static int getWidth(String s, Text txtline) {
		txtline.setText(s);
		return (int) txtline.getWidth();
	}

	public static void gc() {
		Runtime.getRuntime().gc();
	}
}